import { AxiosResponse } from 'axios';
import { Get, Post } from '../axios';


/* 用户登录接口 */
interface LoginInterface {
    (data: {
        name: string,                    /*用户名*/
        password: string                 /*用户密码*/
    }): Promise<AxiosResponse<any>>
}
export const login: LoginInterface = async (data) => {
    try {
        return await Post({ url: "/login", data });
    } catch (error) {
        throw error
    }
};


/* 搜索用户 */
interface SearchInterface {
    (data: {
        name: string                     /*用户名*/
    }): Promise<AxiosResponse<any>>
}
export const search: SearchInterface = async (data) => {
    try {
        return await Get({ url: "/user/getone", data });
    } catch (error) {
        throw error
    }
}


/* 查询用户 */
interface SelectInterface {
    (data: {
        pageNo: number,                  /*当前页数*/
        pageSize: number                 /*每页显示条数*/
        id?: string | null
    }): Promise<AxiosResponse<any>>
}
export const select: SelectInterface = async (data) => {
    try {
        return await Post({ url: "/user/getuser", data });
    } catch (error) {
        throw error
    }
};


/* 修改密码 */
interface UpdataInterface {
    (data: {
        name: string,                    /*用户名*/
        password: string,                /*旧密码*/
        setpass: string                  /*新密码*/
    }): Promise<AxiosResponse<any>>
}
export const updata: UpdataInterface = async (data) => {
    try {
        return await Post({ url: "/user/setpass", data });
    } catch (error) {
        throw error
    }
};


/* 修改余额 */
interface UpuserInterface {
    (data: {
        id: string,                       /*用户id*/
        privateprice?: number,            /*私有价格*/
        publicprice?: number,             /*公有价格*/
        dowtimes?: number                 /*分发下载次数*/
        private?: number                  /*分发下载次数*/
        public?: number                  /*分发下载次数*/
        rolename?: string                /* 权限 */
    }): Promise<AxiosResponse<any>>
}
export const upuser: UpuserInterface = async (data) => {
    try {
        return await Post({ url: "/user/setuser", data });
    } catch (error) {
        throw error
    }
};


/* 删除用户 */
interface DeleteInterface {
    (data: {
        id: string                       /*用户id*/
    }): Promise<AxiosResponse<any>>
}
export const deletes: DeleteInterface = async (data) => {
    try {
        return await Post({ url: "/user/deluser", data });
    } catch (error) {
        throw error
    }

};


/* 注册用户 */
interface RegisterInterface {
    (data: {
        name: string,                    /*用户名*/
        password: string,                /*用户密码*/
        rolename: Role                   /*用户角色*/
    }): Promise<AxiosResponse<any>>
}
export enum Role {
    '超级管理员' = "超级管理员",
    '高级用户' = "高级用户",
    '普通用户' = '普通用户'
}
export const register: RegisterInterface = async (data) => {
    try {
        return await Post({ url: "/user/adduser", data });
    } catch (error) {
        throw error
    }
};
